<?php
/**
 * 签约纪录增加车主id
 * @author angl
 */

date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

$db = init_db();
$list = $db->get('signature_log');
logUtil("共".count($list)."条纪录",'INFO');
$counter = 0;
foreach($list as $kl=>$vl){
    $db->join('cars c',"o.car_id=c.car_id","INNER");
    $owner_id = $db->where('o.order_id',$vl['order_id'])->getValue('order o','c.owner_id');
    $ret = $db->where('signature_id',$vl['signature_id'])->update('signature_log',[
        'owner_id'=>$owner_id
    ]);
    if($ret){
        logUtil("order: ".$vl['order_id']." 处理完成",'INFO');
        $counter++;
    }
}
logUtil($counter." 条数据处理完成","INFO");
